package com.sh.shanhuo.activity;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;

import com.sh.shanhuo.R;
import com.sh.shanhuo.adapter.SortAdapter;
import com.sh.shanhuo.database.sort.SortInfoDBUtils;
import com.sh.shanhuo.database.sort.SortInfoEntity;
import com.sh.shanhuo.database.web.WebInfoDBUtils;
import com.sh.shanhuo.database.web.WebInfoEntity;
import com.sh.shanhuo.dialog.CommonEditDialog;
import com.sh.shanhuo.listener.ItemDragCallback;
import com.sh.shanhuo.listener.SimpleResultCallback;
import com.sh.shanhuo.utils.Tools;
import com.sh.shanhuo.view.CommonItemDecoration;
import com.sh.shanhuo.view.ViewAnimUtils;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;

public class SortEditActivity extends BaseActivity {

    List<SortInfoEntity> sortInfoEntities;
    private SortAdapter sortAdapter;
    //private TextView editorTips;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sort_edit;
    }

    @Override
    @SuppressLint({"UseCompatLoadingForDrawables", "NotifyDataSetChanged"})
    protected void initView() {
        super.initView();
        sortInfoEntities = SortInfoDBUtils.getAll(this);
        SwipeRecyclerView sortList = findViewById(R.id.sortList);
        //editorTips = findViewById(R.id.editorTips);
        sortAdapter = new SortAdapter(this);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        sortList.setLayoutManager(manager);


        SwipeMenuCreator mSwipeMenuCreator = (leftMenu, rightMenu, position) -> {
            if (position == 0)
                return;
            @SuppressLint("ResourceAsColor")
            SwipeMenuItem edit = new SwipeMenuItem(this)
                    .setText("编辑")
                    .setTextColor(getResources().getColor(R.color.white, null))
                    .setTextSize(14)
                    .setWidth(Tools.dp2px(mContext, 79))
                    .setHeight(Tools.dp2px(mContext, 57))
                    .setBackground(R.color.item_check);
            leftMenu.addMenuItem(edit);
            List<WebInfoEntity> webInfoEntities =
                    WebInfoDBUtils.getWebInfoBySortId(mContext, sortInfoEntities.get(position).sortName);
            if (webInfoEntities.size() > 0)
                return;

            @SuppressLint("ResourceAsColor")
            SwipeMenuItem delete = new SwipeMenuItem(this)
                    .setText("删除")
                    .setTextColor(getResources().getColor(R.color.white, null))
                    .setTextSize(14)
                    .setWidth(Tools.dp2px(mContext, 79))
                    .setHeight(Tools.dp2px(mContext, 57))
                    .setBackground(R.color.error);
            leftMenu.addMenuItem(delete);
        };
        sortList.setItemAnimator(new DefaultItemAnimator());
        sortList.setSwipeMenuCreator(mSwipeMenuCreator);

        //sortAdapter.setCheckListener((buttonView, isChecked) -> sortList.setSwipeItemMenuEnabled(!isChecked));

        sortList.setOnItemMenuClickListener((menuBridge, adapterPosition) -> {
            SortInfoEntity sortInfoEntity = sortInfoEntities.get(adapterPosition);
            List<WebInfoEntity> webInfoEntities = WebInfoDBUtils.getWebInfoBySortId(SortEditActivity.this, sortInfoEntity.sortName);
            if (menuBridge.getPosition() == 0) {
                CommonEditDialog.addSortDialog(sortInfoEntity.sortName,
                        SortEditActivity.this, new SimpleResultCallback() {
                            @Override
                            public void onResult(String data1, String data2) {

                            }
                            @Override
                            public void onButton(String action) {
                                sortInfoEntity.sortName = action;
                                SortInfoDBUtils.insertSortInfo(mContext, sortInfoEntity);
                                sortInfoEntities = SortInfoDBUtils.getAll(mContext);
                                for (WebInfoEntity webInfoEntity : webInfoEntities) {
                                    webInfoEntity.sortName = action;
                                    WebInfoDBUtils.insertWebInfo(SortEditActivity.this, webInfoEntity);
                                }
                                sortAdapter.setGroupData(sortInfoEntities);
                            }

                            @Override
                            public void onBean(Object obj) {

                            }
                        });
            }

            if (menuBridge.getPosition() == 1) {
                SortInfoDBUtils.delete(mContext, sortInfoEntity);
                sortInfoEntities = SortInfoDBUtils.getAll(mContext);
                sortAdapter.setGroupData(sortInfoEntities);
            }
            menuBridge.closeMenu();
        });
        sortList.addItemDecoration(new CommonItemDecoration(Tools.dp2px(mContext, 10),
                Tools.dp2px(mContext, 10), Tools.dp2px(mContext, 10)));
        /*sortList.setOnItemLongClickListener((view, adapterPosition) -> {
            ViewAnimUtils.setVisible(editorTips, ViewAnimUtils.Anim.TOP_TO_BOTTOM, 1000);
            handler.sendEmptyMessageDelayed(100, 2000);
        });*/
        sortAdapter.setGroupData(sortInfoEntities);
        sortList.setAdapter(sortAdapter);

        ItemDragCallback callback = new ItemDragCallback(sortAdapter);
        ItemTouchHelper helper = new ItemTouchHelper(callback);
        helper.attachToRecyclerView(sortList);

        ImageView back = findViewById(R.id.back);
        ImageView add = findViewById(R.id.add);
        back.setOnClickListener(v -> finish());
        add.setOnClickListener(v -> add());
    }

    private void add() {
        CommonEditDialog.addSortDialog("", this, new SimpleResultCallback() {
            @Override
            public void onResult(String data1, String data2) {

            }

            @Override
            public void onButton(String action) {
                SortInfoEntity sortInfoEntity = new SortInfoEntity();
                sortInfoEntity.sortName = action;
                sortInfoEntity.sequence = SortInfoDBUtils.getAll(SortEditActivity.this).size();
                SortInfoDBUtils.insertSortInfo(SortEditActivity.this, sortInfoEntity);
                sortInfoEntities = SortInfoDBUtils.getAll(SortEditActivity.this);
                sortAdapter.setGroupData(sortInfoEntities);
            }

            @Override
            public void onBean(Object obj) {

            }
        });
    }

    /*private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 100) {
                ViewAnimUtils.setGone(editorTips, ViewAnimUtils.Anim.BOTTOM_TO_TOP, 1000);
            }
            return false;
        }
    });*/
}